package com.sanpu.kxe.business.login.presenter;

import android.content.Intent;
import android.text.TextUtils;

import com.sanpu.kxe.business.login.bean.LoginOutput;
import com.sanpu.kxe.business.login.contract.LoginConfig;
import com.sanpu.kxe.business.login.contract.LoginContract;
import com.sanpu.kxe.business.login.model.LoginModel;
import com.sanpu.kxe.business.login.view.SetPasswordActivity;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.provider.IMainProvider;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.utils.IntentTool;
import com.sanpu.kxe.utils.PublicProviderUtil;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.kxe.utils.VerifyUtil;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：登陆present
 * Created by 薛勇军 on 2018/6/12.
 */

public class LoginPresenter implements LoginContract.Presenter {

    private LoginContract.View mView;

    private CompositeSubscription mSubscription;

    public LoginPresenter(LoginContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void login(String userName, String password) {
        if (TextUtils.isEmpty(userName)) {
            ToastUtil.showTextViewPromptShort("手机号码不能为空");
            return;
        }
        if (VerifyUtil.checkPhoneNumber(userName)) {
            ToastUtil.showTextViewPromptShort("手机号码不正确");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtil.showTextViewPromptShort("密码不能为空");
            return;
        }
        mView.showLoadingDialog(true, "登录中...");
        Subscription subscription = LoginModel.getInstance().login(mView.getContext(), userName, password)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginOutput>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(LoginOutput loginOutput) {
                        if (loginOutput != null) {
                            CacheManage.getInstance().saveCache(CacheModel.LOGIN_KEY, loginOutput);
                            getUserCenter();
                        } else {
                            mView.dismissLoadingDialog();
                            ToastUtil.showTextViewPrompt("返回数据异常，登录失败!");
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    /**
     * 获取基本信息
     */
    private void getUserCenter() {
        final IMainProvider provider = PublicProviderUtil.getProvider(IMainProvider.class);
        if (provider != null) {
            Subscription subscription = provider.userCenter()
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<UserCenterOutput>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            ToastUtil.showTextViewPrompt("登录失败");
                            mView.dismissLoadingDialog();
                        }

                        @Override
                        public void onNext(UserCenterOutput output) {
                            mView.dismissLoadingDialog();
                            if (output != null) {
                                CacheManage.getInstance().saveCache(CacheModel.USER_CENTER_KEY, output);
                                provider.openMainActivity(mView.getContext(), true);
                            }
                        }
                    });
            mSubscription.add(subscription);
        }
    }

    @Override
    public void forgetPwd() {
        Intent intent = new Intent(mView.getContext(), SetPasswordActivity.class);
        intent.putExtra(LoginConfig.PWD_TYPE, LoginConfig.FORGET_PWD);
        IntentTool.startActivity(mView.getContext(), intent);
    }

    @Override
    public void onDestroyPresenter() {
        if (mView != null)
            mView = null;
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
    }
}
